package com.northpool.operator.transfer;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.ParamEnum;
import com.northpool.operator.annotation.ParamEnums;
import com.northpool.operator.common.stpclient.StpClientOperatorBase;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.resources.datasource.ogr.FGDBDataSource;
import com.northpool.resources.datasource.ogr.ShapeDataSource;
import com.northpool.resources.datatable.ITable;
import com.northpool.stpclient.bean.BaseResponse;
import com.northpool.stpclient.bean.datatransfer.TableCopyBuildPlan;
import com.northpool.stpclient.bean.datatransfer.TbaleCopyModeEnum;
import com.northpool.stpclient.modules.geoprocess.datatransfer.IDataTransferOperator;

import java.io.File;

@OperatorInfo(
        ref = "Shp2FgdbOperator",
        name = "Shape转Fgdb",
        description = "",
        enums = {
                @ParamEnums(ref = "mode", value = {
                        @ParamEnum(ref = "REPLACE", name = "删除重建"),
                        @ParamEnum(ref = "MERGE", name = "合并"),
                        @ParamEnum(ref = "APPENDER", name = "追加"),
                })
        }
)
public class Shp2FgdbOperator extends StpClientOperatorBase {
    String shpDirPath;
    String shpName;

    String gdbPath;
    String layerName;

    String mode;

    @InputParams({
            @InputParam(ref = "shpDirPath", name = "shp文件夹", desc = "目标文件夹,应为共享文件夹", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "shpName", name = "shp文件名", desc = "shp文件名, 和'shp文件夹'拼接成完整路径", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "gdbPath", name = "gdb文件路径", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "layerName", name = "图层名称", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "mode", name = "传输类型", type = TypeEnum.STRING, value = "REPLACE"),
    })
    public Shp2FgdbOperator() {
    }

    @Override
    public void execute(ParamTool params) throws Exception {
        shpDirPath = params.getStr("shpDirPath");
        shpName = params.getStr("shpName");
        gdbPath = params.getStr("gdbPath");
        layerName = params.getStr("layerName");
        mode = params.getStr("mode");

        IDataTransferOperator transferOperator = this.client.dataTransferOperator();

        ShapeDataSource originDataSource = new ShapeDataSource(shpDirPath + File.separator + shpName + ".shp");

        FGDBDataSource targetDataSource = new FGDBDataSource(gdbPath);

        ITable table = originDataSource.getTable(this.shpName);
        Integer srid = table.spatialInfo().getSrid();


        TableCopyBuildPlan plan = TableCopyBuildPlan.TableCopyParamsBuilder.create()
                .addSrid(srid)
                // 设置来源数据源
                .addOriginDatasource(originDataSource)
                // 设置来源表
                .addOriginTableName(this.shpName)
                // 设置目标数据源
                .addTargetDatasource(targetDataSource)
                // 设置目标表名
                .addTargetTableName(this.layerName)
                .addMode(TbaleCopyModeEnum.REPLACE)
                .build();
        BaseResponse response = transferOperator.startTableCopy(plan);
        String requestId = response.getRequestId();
        transferOperator.onStart(requestId, () -> {
            log("开始数据传输");
        });
        transferOperator.onError(requestId, () -> {
            error(transferOperator.log(requestId));
            this.failedExit();
        });
        transferOperator.onSuccess(requestId, this::successExit);
        this.waiting(24);
    }
}
